package cn.classg.redisson.factory;

import cn.classg.redisson.enums.LockType;
import cn.classg.redisson.helpers.ApplicationContextHelper;
import cn.classg.redisson.service.LockService;
import cn.classg.redisson.service.impl.*;

import java.util.EnumMap;

/**
 * 锁处理工厂类
 * @author CLASSG
 */
public class LockServiceFactory {

    private static final EnumMap<LockType, Class<?>> serviceMap = new EnumMap<>(LockType.class);

    // 缓存对应的锁类型（策略模式）
    static {
        serviceMap.put(LockType.REENTRANT, ReentrantLockServiceImpl.class);
        serviceMap.put(LockType.FAIR, FairLockServiceImpl.class);
        serviceMap.put(LockType.READ, ReadLockServiceImpl.class);
        serviceMap.put(LockType.WRITE, WriteLockServiceImpl.class);
        serviceMap.put(LockType.RED, RedLockServiceImpl.class);
    }

    /**
     * 根据类型获取不同的锁实现
     *
     * @param lockType 锁类型
     * @return LockService
     */
    public LockService getLock(LockType lockType) {
        return (LockService) ApplicationContextHelper.getContext().getBean(serviceMap.get(lockType));
    }

}